package com.demo.petshop.service.impl;

import com.demo.petshop.entity.VideoComments;
import com.demo.petshop.mapper.VideoCommentsMapper;
import com.demo.petshop.service.VideoCommentsService;
import com.demo.petshop.util.Result;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.sqlite.date.DateFormatUtils;

import java.util.Date;

/**
 *  服务层实现。
 *
 * @author lenovo
 * @since 2025-06-11
 */
@Service
public class VideoCommentsServiceImpl extends ServiceImpl<VideoCommentsMapper, VideoComments> implements VideoCommentsService {

    @Autowired
    private VideoCommentsMapper videoCommentsMapper;

    private int getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser().getUserId();
    }

    @Override
    public Object addComment(Integer videoId, String content) {
        return Result.success(videoCommentsMapper.insert(
                new VideoComments(null, videoId, getUserId(), content, 0,
                        DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"))));
    }

    @Override
    public Object like(Integer commentId) {
        VideoComments videoComments = videoCommentsMapper.selectOneById(commentId);
        videoComments.setLikes(videoComments.getLikes() + 1);
        return Result.success(videoCommentsMapper.update(videoComments));
    }
}
